﻿using System;
using System.Collections.Generic;
using Xunit;

namespace UniSender.Test
{
    using Dic = Dictionary<string, string>;

    public class TestRequest
    {
        Uri baseUrl = new Uri(UniSenderRequest.HOST + "/tt/api/action");

        [Fact]
        public void TestUrlBuilder_WithoutArguemnts()
        {
            var request = new UniSenderRequest("tt", "action", "test");
            TestUtility.CompareUrl(baseUrl, new Uri(request.GetUrl(false)));
            TestUtility.CompareUrl(new Uri(baseUrl, "?format=json&api_key=test"), new Uri(request.GetUrl(true)));
        }

        [Fact]
        public void TestUrlBuilder_WithArguments()
        {
            var request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>()
            {
                { "arg1", 1 },
                { "arg2", "value" },
                { "empty", null }
            });
            TestUtility.CompareUrl(baseUrl, new Uri(request.GetUrl(false)));
            TestUtility.CompareUrl(new Uri(baseUrl, "?format=json&api_key=test&arg1=1&arg2=value"), new Uri(request.GetUrl(true)));
        }

        [Fact]
        public void TestQueryBuilder_WithArray()
        {
            var request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>()
            {
                { "list_ids", new int[] { 123456, 345678, 2344423 } }
            });
            TestUtility.CompareUrlQuery("format=json&api_key=test&list_ids=123456,345678,2344423", request.GetQuery());
        }

        [Fact]
        public void TestQueryBuilder_WithExpando()
        {
            var request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>()
            {
                {
                    "fields",
                    new Dictionary<string, object>()
                    {
                        { "email", "test@mail.ru" },
                        { "phone", "79201234567" },
                        { "Name", "John Smith" },
                        { "empty", null }
                    }
                }
            });
            TestUtility.CompareUrlQuery("format=json&api_key=test&fields[email]=test%40mail.ru&fields[phone]=79201234567&fields[Name]=John+Smith", request.GetQuery());
        }

        [Fact]
        public void TestQueryBuilder_WithDeepExpando()
        {
            var request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>()
            {
                { "data", new Dictionary<string, object>() {
                    { "0", 0 },
                    { "1", 1 },
                    { "2", new Dictionary<string, object>() {
                        { "0", 10 },
                        { "1", 11 },
                        { "00", null }
                    } },
                    { "-1", null }
                } }
            });
            TestUtility.CompareUrlQuery("format=json&api_key=test&data[0]=0&data[1]=1&data[2][0]=10&data[2][1]=11", request.GetQuery());
        }

        [Fact]
        public void TestQueryBuilder_Complex()
        {
            var request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>()
            {
                { "list_ids", new int[] { 123456, 345678, 2344423 } },
                {
                    "fields",
                    new Dictionary<string, object>()
                    {
                        { "email", "test@mail.ru" },
                        { "phone", new string[] { "79201234567", "79601234567" } },
                        { "Name", "John Smith" }
                    }
                },
                { "tags", new string[] { "client", "thanks" } },
                { "overwrite", 2 }
            });
            TestUtility.CompareUrlQuery(
                "format=json&api_key=test&list_ids=123456,345678,2344423&fields[email]=test%40mail.ru&fields[phone]=79201234567,79601234567&fields[Name]=John+Smith&tags=client,thanks&overwrite=2",
                request.GetQuery());
        }

        [Fact]
        public void TestQueryBuilder_TestMode()
        {
            UniSenderRequest request;
            
            request = new UniSenderRequest("tt", "action", "test");
            Assert.DoesNotContain("&test_mode=", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test") { TestMode = false };
            Assert.DoesNotContain("&test_mode=", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>() { { "arg1", 1 } }) { TestMode = false };
            Assert.DoesNotContain("&test_mode=", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test") { TestMode = true };
            Assert.Contains("&test_mode=1", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>() { { "arg1", 1 } }) { TestMode = true };
            Assert.Contains("&test_mode=1", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>() { { "test_mode", 4 } }) { TestMode = false };
            Assert.Contains("&test_mode=4", request.GetQuery());

            request = new UniSenderRequest("tt", "action", "test", new Dictionary<string, object>() { { "test_mode", 4 } }) { TestMode = true };
            Assert.Contains("&test_mode=4", request.GetQuery());
            Assert.DoesNotContain("&test_mode=1", request.GetQuery());
        }
    }
}
